package com.tender.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class KafkaProducerController {

    /**
     * 本地搭建的环境已经创建的topic 有如下：
     *
     * my-topic1
     * my-topic2
     * topic1
     * topic2
     * tender1
     * tender2
     */

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    @GetMapping("/send/msg")
    public String sendMsg(@RequestParam String message){
        try {
            // 需要提前建立好 my-topic2
            kafkaTemplate.send("my-topic2", message);
            System.out.println("消息发送成功...");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

}
